# -*- coding: utf-8 -*-
import cgi
import json
import time
#import datetime
import pywintypes
# First import two useful modules
from win32com.client import Dispatch
#from adoconstants import *

print 'Content-type: application/json'
print
form=cgi.FieldStorage()
s='[{"id":1,"name":"测试1ab c"},{"id":2,"name":"测试1ab c"}]'
#print form["id"].value


# Create the ADO Connection object via COM.
oConn = Dispatch('ADODB.Connection')

# Now set the connection properties via the ConnectionString
# We're connecting to a SQL Server on 192.168.1.100 using OLEDB.
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data\data.mdb;Jet OLEDB:Database Password=tp@chenkai"

# Now open the connection
oConn.Open()

# Instead of setting the ConnectionString and then calling Open, it is also
# possible to call the Open method directly and pass the connection string 
# as an argument to the method. {i.e.)
# oConn.Open("Provider=SQLOLEDB.1; Data Source=.....")

# Now create a RecordSet object and open a table
oRS = Dispatch('ADODB.RecordSet')
#oRS.ActiveConnection = oConn    # Set the recordset to connect thru oConn
#sql="select * from cd where cdid="+form["id"].value
sql="select * from cl"
oRS.Open(sql,oConn)             # Open a table called zipcode
#oRS.Open(sql,oConn,1,1)             # Open a table called zipcode
# Can also use oRS.Open("zipcode", oConn) instead of setting ActiveConnection
data = [ { 'id':'A', 'b':(2, 4), 'name':'测试1ab 从' },{ 'id':'A', 'b':(2, 4), 'name':3.0 } ]
l=[]
pytime = type(pywintypes.Time(time.time ()))
while not oRS.EOF:
	d={}
	for x in range(oRS.Fields.Count):
		if type(oRS.Fields(x).Value)==pytime:
			d[oRS.Fields(x).Name]=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(oRS.Fields(x).Value)))
		else:
			d[oRS.Fields(x).Name]=oRS.Fields(x).Value

	# d["cph"]=oRS.Fields("cph").Value
	# d["clm"]=oRS.Fields("clm").Value
	# d["cl_lat"]=oRS.Fields("cl_lat").Value
	# d["cl_lng"]=oRS.Fields("cl_lng").Value
	# d["clid"]=oRS.Fields("clid").Value
	# d["sscd"]=oRS.Fields("sscd").Value
	# d["tel"]=oRS.Fields("tel").Value
	
	l.append(d)
	#print d
	oRS.MoveNext()
#j=json.dumps(data,ensure_ascii=False)
j=json.dumps(l)
print j
oRS.Close()
oRS=None
oConn.Close()
oConn = None   
